package lab.poc.erl;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

class TPExecutor extends ThreadPoolExecutor {

	TPExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,
			TimeUnit unit, BlockingQueue<Runnable> workQueue,
			ThreadFactory threadFactory, RejectedExecutionHandler handler) {

		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
				threadFactory, handler);
	}

	@Override
	protected void afterExecute(final Runnable r, final Throwable t) {
		if (r instanceof RunnableProcess) {
			((RunnableProcess) r).dissolveFromThread();
		}
		Pid.PID.remove();
		Actor.MBOX.remove();
	}

	@Override
	protected void beforeExecute(final Thread t, final Runnable r) {
		if (r instanceof RunnableProcess) {
			final RunnableProcess proc = (RunnableProcess) r;
			Pid.PID.set(proc.getPid());
			Actor.MBOX.set(proc.getMailbox());
		}
	}
}
